package com.cedifie.lendapp.ui.launch

import android.view.View
import com.cedifie.lendapp.analysis.TestAnalysisInit
import com.cedifie.lendapp.databinding.ActSplashBinding
import com.cedifie.lendapp.http.ApiService
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.http.HttpHostDns
import com.cedifie.lendapp.mvp.MvpAct
import com.cedifie.lendapp.ui.launch.impl.CedLaunchPresenterImpl
import com.cedifie.lendapp.ui.launch.view.ICedLaunchView
import com.cedifie.lendapp.ui.loan.act.CedLoanMainAct
import com.cedifie.lendapp.ui.user.act.CedUserLoginAct
import com.cedifie.lendapp.utils.event.CedEventType
import com.cedifie.lendapp.utils.helper.SharedHelper
import com.lib.base.utils.TitleBarUtil
import com.lib.base.utils.helper.ActivityStackManager
import com.lib.base.utils.log.LogHelper
import java.util.TimeZone

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/12 11:16
 * @Annotation：启动页
 */
class CedLaunchAct: MvpAct<ICedLaunchView, CedLaunchPresenterImpl>(),ICedLaunchView {
    private lateinit var binding: ActSplashBinding

    override fun getCedRootView(): View {
        binding = ActSplashBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createMvpPresenter(): CedLaunchPresenterImpl {
        return CedLaunchPresenterImpl(this)
    }

    override fun initCedView() {
        TitleBarUtil.setStatusBarLightMode(this, false)
        isInterceptBack(true)
        //关闭所有页面
        ActivityStackManager.finishAllActivity(CedLaunchAct::class.java)
    }

    override fun initCedListener() {
    }

    override fun initCedData() {
        // TODO: 坦桑时区需要通过域名获取到IP之后再调时区接口
        if (TimeZone.getDefault().id == "Africa/Dar_es_Salaam"){
            getHttpHostDNSIpResolver()
        } else {
            mvpPresenter?.loadCedTimeZoneAction()
        }
    }

    /**
     * ip解析
     */
    private fun getHttpHostDNSIpResolver() {
        try {
            HttpHostDns().getHttpIPAddressAction(ApiService.CED_BASE_URL) { ipAddress ->
                LogHelper.e("ip address is $ipAddress")
                ApiService.CED_BASE_URL = "https://$ipAddress"
                mvpPresenter?.loadCedTimeZoneAction()
            }
        } catch (e: Exception){
            mvpPresenter?.loadCedTimeZoneAction()
        }
    }

    /**
     * 地区转发成功
     */
    override fun showCedTimeZoneRequestData(timeZone: String, zoneCode: String) {
        //保存转发地区
        SharedHelper.saveCedSPData(SharedHelper.CED_SPKEY.CEDIFIE_TIMEZONE,timeZone)
        //保存转发地区 区号
        SharedHelper.saveCedSPData(SharedHelper.CED_SPKEY.CEDIFIE_TIMEZONE_CODE,zoneCode)
        //初始化analysis lib 赋值 转发地区
        TestAnalysisInit.initAnalysisLibAtZoneSuccess()
        //添加埋点
        HttpHelper.addCedEventAction(CedEventType.PAGE.CED_PAGE_LAUNCH_START,"")
        mvpPresenter?.loadCedCommonConfigAction()
    }

    /**
     * 进入登录页
     */
    override fun showCedLoginPage() {
        onIntent(CedUserLoginAct::class.java)
        finish()
    }

    /**
     * 进入主页
     */
    override fun showCedMainPage() {
        onIntent(CedLoanMainAct::class.java)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addCedEventAction(CedEventType.PAGE.CED_PAGE_LAUNCH_END,"")
    }
}